var app = getApp();
Page({
    data:{
        curPage : "/zofui_sales/park/jhtindex",
        siteroot : app.siteInfo.siteroot.replace('app/index.php', ''),uniacid: app.siteInfo.uniacid,
        params : {},
        sort : [],
		inited : false,
        userinfo : {},
        options : {},
        list : [],
        areakey : [],
        timer : false,
        iskeyword :true,
        tabtye : 2,
        key : ['粤','','','','','','',''],
        isnew : false,
        actindex : 1,
        history : [],
        isauto : 1,
    },
    onLoad:function(options){
        var self = this;
        var zfid = options.zfid ? options.zfid : (options.scene ? options.scene : 0);
        self.data.options = options;
        
        app.com.auth(function(user){
            var postdata = {
                op : 'info',
                id : options.id,
                zfid : zfid,
            };
            app.com.http('park','POST',postdata,0,false,'','',function(res){
                if(res.data && !res.data.errno){

                    var data = res.data.data;
                    self.setData({
                       sharedata : res.data.data.sharedata,
                       params : res.data.data.params,
                       areakey : res.data.data.areakey,
                       abckey : res.data.data.abckey,
                       history : res.data.data.history ? res.data.data.history : [],
                       inited : true,
                    },function(){
                        if(self.data.history.length > 0){
                            var e = {
                                currentTarget : {
                                    dataset : {
                                        i : 0,
                                    }
                                }
                            }
                            self.uselog(e);
                        }
                    });
                    
                    wx.setNavigationBarTitle({
                        title:  '停车缴费',
                    });
                }else{
                    app.com.alert(res.data.message);
                }
            });
            app.com.setBar(app,self);
        });
    },
    onShareAppMessage : function(){
        var self = this;
        return {
            title: self.data.sharedata.title,
            path: self.data.sharedata.path,
            imageUrl : self.data.sharedata.img,
        }
    },
    onPullDownRefresh: function () {
        this.onLoad(this.data.options);
        wx.stopPullDownRefresh();
    },
    getPhoneNumber( e ){
        var self = this;
        if( e.detail.errMsg == 'getPhoneNumber:ok') {
            var postdata = {
                iv: e.detail.iv,
                encryptedData: e.detail.encryptedData,
                op : 'encrypt',
            };
            app.com.http('user_v1','POST',postdata,0,true,'','',function(res){
                if (res.data && !res.data.errno) {
                    self.setData({
                        'userinfo.tel' : 1,
                    });
                    self.toorder();
                }else{
                    app.com.alert(res.data.message);
                }
            });
        }else{
            app.com.alert('请允许授权'+e.detail.errMsg);
        }
    },
    onClose(){
        this.setData({
            iskeyword : false,
        })
    },
    changekey(e){
        var self = this;
        var name = e.currentTarget.dataset.name;
        var type = e.currentTarget.dataset.type;
        var max = this.data.isnew ? 7 : 6 ;

        if(type == 'change'){
            var status = self.data.tabtye == 1 ? 2 : 1;
            self.setData({
                tabtye : status
            })
        }else if(type == 'del'){

            var next = this.data.actindex <= 0 ? 1 :  this.data.actindex-1;

            this.setData({
                ['key['+this.data.actindex+']'] : '',
                actindex : next,
            })
        }else if(type == 'a'){
            this.setData({
                'key[0]' : name,
            })
        }else{

            var index = this.data.actindex <= 0 ? 1 : (this.data.actindex > max ? 1 : this.data.actindex);
            var next = index >= max ? 1 :  index+1;

            this.setData({
                ['key['+index+']'] : name,
                actindex : next,
            })
        }
    },
    authed(e){
        var self = this;
        self.setData({
            'params.needauth' : 0,
        })
        self.subpark();
    },
    newfn(){
        this.setData({
            isnew : !this.data.isnew
        })
    },
    setact(e){
        var index = e.currentTarget.dataset.index*1;
        this.setData({
           actindex : index,
           isauto : 0,
           tabtye : index >= 1 ? 2 : 1,
           iskeyword : true,
        })
    },
    subpark(){
        var self = this;
        if(self.data.params.needauth*1 == 0){
            var postdata = {
                op : 'subpark',
                isnew : self.data.isnew ? 1 : 0,
                car : JSON.stringify(self.data.key),
            };
            app.com.http('park','POST',postdata,0,true,'','',function(res){
                if (res.data && !res.data.errno) {
                    
                    wx.navigateTo({
                        url : '/zofui_sales/park/pay'
                    })
                }else if(res.data.errno == 2){
                    app.com.confirm(res.data.message,function(){
                        var tourl = '/zofui_sales/ucard/buycard';
                        wx.navigateTo({
                          url: tourl
                        });
                    },false,'取消','成为会员');
                }else{
                    app.com.alert(res.data.message);
                }
            });
        }else{
            self.selectComponent('#loginauth').hideauth();
        }
    },
    uselog(e){
        var i = e.currentTarget.dataset.i;
        var item = this.data.history[i].cararr;
        if(item){
            this.setData({
                key : JSON.parse(JSON.stringify(item)),
                isnew : item.length >= 8 ? 1 : 0,
            })
        }
    },
    location : function( e ){
        app.com.location( e );
    },
    otherapp : function( e ){
        app.com.otherapp( e );
    },
    navigateto : function( e ){
        app.com.navigateto( e );
    },
    footNavigate(e){
        var url = e.currentTarget.dataset.url;
        wx.switchTab({
          url: url
        })
    },
    redirectto : function( e ){
        app.com.redirectto( e );
    },    
    callphone : function(e){
        app.com.callphone( e );
    },
    toweburl : function(e){
        app.com.toweburl( e );
    },
    showimages : function( e ){
        app.com.showimages( e );
    }
    

})